﻿using System;
using System.Web.Mvc;
using Framework.Admin;
using Framework.Util.Cryptography;
using Project.DAL.Repository;
using Project.Domain.Entities;

namespace Project.WebUI.Areas.Admin.Controllers
{
    public class DashBoardController : Controller
    {
        #region "private"
        private readonly IUserRepository _userRepository;
        private readonly IDepositRepository _depositRepository;
        private readonly IWithDrawRepository _withDrawRepository;
        private readonly IUserService _userService;
        private readonly ICountryRepository _countryRepository;
        private readonly IContactRepository _contactRepository;        
        private readonly IStoreSecureRepositoty _storeSecureRepositoty;

        public DashBoardController(
            IUserRepository userRepository,
            IDepositRepository depositRepository,
            IWithDrawRepository withDrawRepository,
            IUserService userService,
            ICountryRepository countryRepository,
            IContactRepository contactRepository,            
            IStoreSecureRepositoty storeSecureRepositoty)
        {
            _userRepository = userRepository;
            _depositRepository = depositRepository;
            _withDrawRepository = withDrawRepository;
            _userService = userService;
            _countryRepository = countryRepository;
            _contactRepository = contactRepository;            
            _storeSecureRepositoty = storeSecureRepositoty;
        }

        #endregion        

        public ActionResult Index()
        {
            ViewBag.Active = "active";
            var currentAdmin = _userRepository.Get(ad => ad.UserName.Equals("admin") && ad.IsAdmin);
            if (currentAdmin != null)
            {
                if (Session["admin"] == null)
                {
                    return RedirectToAction("Login");
                }
            }
            else
            {
                var admin = new User()
                {
                    UserName = "admin",
                    Password = CryptographyManager.Encrypt("123456"),
                    IsAdmin = true,
                    NumPin = 1,
                    CreatedTime=DateTime.Now,
                    Active = true
                };
                _userRepository.Add(admin);
                _userRepository.SaveChanges();
                return RedirectToAction("Login");
            }

            return View();
        }        
              
        #region "Admin Region"

        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [ValidateInput(true)]
        public ActionResult Login(FormCollection fc)
        {
            var userName = Request.Params["user"];
            var passWord = Request.Params["pass"];
            passWord = CryptographyManager.Encrypt(passWord);
            var user = _userRepository.Get(u => u.UserName.Equals(userName) && u.IsAdmin
                                                && u.Password.Equals(passWord)
                );
            if (user == null)
            {
                return Content("0");
            }
            Authentication.SigIn("admin", true);
            Session["admin"] = "admin";
            return Content("1");
        }

        public ActionResult LogOut()
        {
            Authentication.SignOut();
            Session.Abandon();
            return RedirectToAction("Index", "Home", new {area = ""});
        }

        #endregion   
 
        public ActionResult UpdateProfile()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UpdateProfile(User userModel)
        {
            return View();
        }
    }
}

